package com.atwj.controller;

import com.atwj.Service.BookService;
import com.atwj.Service.impl.BookServiceImpl;
import com.atwj.entity.Book;
import com.atwj.entity.Cart;
import com.atwj.entity.CartItem;
import com.atwj.utils.webUtil;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 吴先森
 * @description:
 * @create 2022-03-29 20:56
 */
public class CartServlet extends BaseServlet {
    BookService bookService = new BookServiceImpl();

    /**
     * 修改商品数量
     *
     * @param req
     * @param resp
     * @author 吴先森
     * @date 2022/3/30 21:21
     */
    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数，商品的编号，商品的数量
        int id = webUtil.parseInt(req.getParameter("id"), 0);
        int count = webUtil.parseInt(req.getParameter("count"), 1);

        //获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");

        if (cart != null) {
            //修改数量
            cart.updateCount(id, count);
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

    /**
     * 清空购物车
     *
     * @param req
     * @param resp
     * @author 吴先森
     * @date 2022/3/30 21:12
     */
    protected void clearItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            cart.clear();
            // 重定向回原来购物车展示页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

    /**
     * 删除商品项
     *
     * @param req
     * @param resp
     * @author 吴先森
     * @date 2022/3/30 14:56
     */
    protected void deleteItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取商品编号
        int id = webUtil.parseInt(req.getParameter("id"), 0);
        // 获取购物车对象
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart != null) {
            // 删除 了购物车商品项
            cart.deleteItem(id);
            // 重定向回原来购物车展示页面
            resp.sendRedirect(req.getHeader("Referer"));
        }
    }

    /**
     * 加入购物车
     *
     * @param req
     * @param resp
     * @author 吴先森
     * @date 2022/3/29 20:59
     */
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的编号，商品编号
        int id = webUtil.parseInt(req.getParameter("id"), 0);
        //调用bookService.queryBookById(id):book得到图书信息，并转换成CartItem类
        Book book = bookService.queryBookByid(id);
        CartItem cartItem = new CartItem(book.getId(), book.getName(), 1, book.getPrice(), book.getPrice());
        //调用cart.addItem() 添加商品项
        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            req.getSession().setAttribute("cart", cart);
        }
        cart.addItem(cartItem);
        //重定向商品列表页面
//        resp.sendRedirect(req.getHeader("Referer"));
        req.getSession().setAttribute("lastName", cartItem.getName());
        //利用ajax，回显信息
        Map<String, Object> resultMap = new HashMap<>();

        resultMap.put("totalCount", cart.getTotalCount());
        resultMap.put("lastName", cartItem.getName());

        Gson gson = new Gson();
        String resultMapString = gson.toJson(resultMap);

        resp.getWriter().write(resultMapString);
    }
}
